package com.sykj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sykj.dto.ProductCategoryDto;
import com.sykj.dto.ProductInfoDto;
import com.sykj.entity.ProductCategory;
import com.sykj.entity.ProductInfo;
import com.sykj.mapper.ProductInfoMapper;
import com.sykj.service.IProductCategoryService;
import com.sykj.service.IProductInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sykj
 * @since 2019-06-28
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private IProductCategoryService categoryService;

    @Override
    public Map<String, Object> queryProduct() {
        Object o = redisTemplate.opsForValue().get("productInfo:product");
        Object category = redisTemplate.opsForValue().get("productInfo:category");
        Map<String, Object> map = new HashMap<>(2);
        List<ProductCategory> categories = null;
        List<ProductInfo> infos = null;
        // 判断缓存中是否有热销商品
        if (o == null || category == null) {
            // 查出所有类型
            categories = categoryService.list();
            QueryWrapper<ProductInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("category_type", 1);
            wrapper.eq("product_status", 0);
            // 查出热销商品
            infos = this.baseMapper.selectList(wrapper);
            // 放入redis中
            redisTemplate.opsForValue().set("productInfo:product", infos);
            redisTemplate.opsForValue().set("productInfo:category", categories);
        } else {
            categories = (List<ProductCategory>) category;
            infos = (List<ProductInfo>) o;
        }
        // 复制到传输对象中
        List<ProductInfoDto> infoDtos = new ArrayList<>();
        infos.forEach(info -> {
            ProductInfoDto productInfoDto = new ProductInfoDto();
            BeanUtils.copyProperties(info, productInfoDto);
            // 设置默认数量为0
            productInfoDto.setProductQuantity(0);
            infoDtos.add(productInfoDto);
        });

        // 复制到传输对象中
        List<ProductCategoryDto> list1 = new ArrayList<>();
        categories.forEach(category1 -> {
            ProductCategoryDto categoryDto = new ProductCategoryDto();
            BeanUtils.copyProperties(category1, categoryDto);
            list1.add(categoryDto);
        });

        // 存入map中
        map.put("products", infoDtos);
        map.put("category", list1);
        return map;
    }

    @Override
    public Map<String, Object> queryAll() {
        Map<String, Object> map = new HashMap<>();
        List<ProductInfo> infos = this.baseMapper.selectList(null);
        List<ProductCategory> list = categoryService.list();
        map.put("food", infos);
        map.put("type", list);
        return map;
    }
}
